<?php

namespace App\Admin\Controllers;

use App\User;
use App\UserFenshu;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserFenshuController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户分数';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserFenshu());
        $grid->model()->join('5kcrm_admin_access','user_fenshu.user_id','=','5kcrm_admin_access.user_id')

            ->where('5kcrm_admin_access.group_id','=',8)
            ->orderBy('time','desc');
        $grid->column('id', __('Id'));
        $grid->column('user_id', __('用户'))->display(function ($item){
            return User::find($item)->realname;
        });
        $grid->column('fenshu', __('分数'));
        $grid->column('chengjiao', __('成交次数'));
        $grid->column('genjin', __('跟进次数'));
        $grid->column('daofang', __('到访次数'));
        $grid->column('xinren', __('新人'));
        $grid->column('lead', __('所得线索次数'))->sortable();
        $grid->column('time','时间');
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter){
            $filter->equal('time','时间')->datetime(['format' => 'YYYY-MM']);

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserFenshu::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('fenshu', __('Fenshu'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserFenshu());

        $form->number('user_id', __('User id'));
        $form->number('fenshu', __('Fenshu'));

        return $form;
    }
}
